package com.zgw.his.emr.workbench;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.part.ViewPart;

import com.zgw.his.emr.model.RootRecord;
import com.zgw.his.emr.parser.XmlParser;
import com.zgw.his.emr.util.ViewUtil;
import com.zgw.his.emr.widget.MedicalPage;

/**
 * 默认的页页容器</p> 其实现方式通过{@link PageView} 装载 {@link MedicalPage}
 * 
 * @author Z.G.W
 * */
public class DefaultContainer extends MedicalContainer {
	private List<ViewPart> pageViews = new ArrayList<ViewPart>();

	public RootRecord parser() throws Exception {
		XmlParser parser = new XmlParser(content);
		record = (RootRecord) parser.parser();
		return record;
	}

	@Override
	public MedicalPage createPage() {
		return new MedicalPage();
	}

	@Override
	public void clear() {
		super.clear();
		for (ViewPart view : pageViews) {
			ViewUtil.getActivePage().hideView(view);
		}
		pageViews.clear();
	}

	// 显示页
	@Override
	public void showPage(MedicalPage page) {
		try {
			long start = System.currentTimeMillis();
			PageView p = (PageView) ViewUtil.getActivePage().showView(
					PageView.ID, page.getName() + page.getPageIndex(),
					IWorkbenchPage.VIEW_ACTIVATE);
			p.setName(page.getName());
			if (page.getControl() == null || page.getControl().isDisposed()) {
				p.getContainer().setVisible(false);
				page.createControl(p.getContainer());
				p.layout();
				p.getContainer().setVisible(true);
			}
			if (!pageViews.contains(p))
				pageViews.add(p);
			currentPage = page;
			System.out.println("done: " + (System.currentTimeMillis() - start));
		} catch (Exception e) {
			e.printStackTrace();
		}
		page.setActivate(true);
	}
}
